#include "mergeimg.h"
#include <qdir.h>
MergeImg::MergeImg(QObject *parent) : QObject(parent)
{
}
MergeImg::MergeImg(std::string baseImg, std::vector<vector<cv::Point>> pArr)
{
    dstImg = cv::imread(baseImg);
    copyMakeBorder(dstImg, dstImg, top, bottom, left, right, cv::BORDER_CONSTANT, Scalar(0));
    rotatedpointArr = pArr;
    for (int i = 0; i < rotatedpointArr[0].size();++i)
    {
        rotatedpointArr[0][i].x += left;
    }
    maskpointArr = pArr;
    prevPt = rotatedpointArr[0][3];//初始基准点为一号图的D点
}
void MergeImg::fillMaskPoints(cv::Rect sourceRect,int i)
{
    Point top;//A
    Point rightTop;//B
    Point rightBottom;//C
    Point leftBottom;//D
    Point leftTop;//E
    maskpointArr[i] = { top, rightTop, rightBottom, leftBottom, leftTop };
    for (int j = 0; j < maskpointArr[i].size();++j)
    {
        maskpointArr[i][j].x = rotatedpointArr[i][j].x - sourceRect.x;
        maskpointArr[i][j].y = rotatedpointArr[i][j].y - sourceRect.y;
    }
    /*if (i==1)
    {
    top.x = 0;
    top.y = rotatedpointArr[i][0].y - sourceRect.y;
    rightTop.x = rotatedpointArr[i][1].x - sourceRect.x;
    rightTop.y = 0;
    rightBottom.x = sourceRect.width - 1;
    rightBottom.y = rotatedpointArr[i][2].y-sourceRect.y;
    leftBottom.x = rotatedpointArr[i][3].x - sourceRect.x;
    leftBottom.y = sourceRect.height - 1;
    leftTop.x = rotatedpointArr[i][4].x - sourceRect.x;
    leftTop.y = sourceRect.height - 1;
    }
    else if (i==2)
    {
    top.x = 0;
    top.y = rotatedpointArr[i][0].y - sourceRect.y;
    rightTop.x = rotatedpointArr[i][1].x - sourceRect.x;
    rightTop.y = 0;
    rightBottom.x = rotatedpointArr[i][2].x - sourceRect.x;;
    rightBottom.y = 0;
    leftBottom.x = sourceRect.width - 1;
    leftBottom.y = rotatedpointArr[i][3].y - sourceRect.y;
    leftTop.x = rotatedpointArr[i][4].x - sourceRect.x;
    leftTop.y = sourceRect.height - 1;
    }
    else if (i == 3)
    {
    top.x = rotatedpointArr[i][0].x - sourceRect.x;
    top.y = sourceRect.height - 1;
    rightTop.x = 0;
    rightTop.y = rotatedpointArr[i][1].y - sourceRect.y;
    rightBottom.x = rotatedpointArr[i][2].x - sourceRect.x;
    rightBottom.y = 0;
    leftBottom.x = rotatedpointArr[i][3].x - sourceRect.x;
    leftBottom.y = 0;
    leftTop.x = sourceRect.width-1;
    leftTop.y = rotatedpointArr[i][4].y - sourceRect.y;;
    }
    else if (i == 4)
    {
    top.x = sourceRect.width - 1;
    top.y = rotatedpointArr[i][0].y - sourceRect.y;
    rightTop.x = rotatedpointArr[i][1].x - sourceRect.x;
    rightTop.y = sourceRect.height - 1;
    rightBottom.x = 0;
    rightBottom.y = rotatedpointArr[i][2].y - sourceRect.y;
    leftBottom.x = rotatedpointArr[i][3].x - sourceRect.x;
    leftBottom.y = 0;
    leftTop.x = rotatedpointArr[i][4].x - sourceRect.x;
    leftTop.y = 0;
    }
    else if (i == 5)
    {
    top.x = sourceRect.width-1;
    top.y = rotatedpointArr[i][0].y - sourceRect.y;
    rightTop.x = rotatedpointArr[i][1].x - sourceRect.x;
    rightTop.y = sourceRect.height-1;
    rightBottom.x = rotatedpointArr[i][2].x - sourceRect.x;
    rightBottom.y = sourceRect.height - 1;
    leftBottom.x = 0;
    leftBottom.y = rotatedpointArr[i][3].y - sourceRect.y;
    leftTop.x = rotatedpointArr[i][4].x - sourceRect.x;
    leftTop.y = 0;
    }
    else
    {
    cout << "void MergeImg::fillMaskPoints err,Not index exist!!!" << endl;
    }*/

}
Point MergeImg::GetResultPt(cv::Rect sourceRect,int i)
{
    Point result;
    if (i==1)
    {
        result.x = prevPt.x - sourceRect.width;
        result.y = prevPt.y - maskpointArr[i][2].y;
        //更新prevPt
        prevPt.x = maskpointArr[i][3].x + result.x;
        prevPt.y = maskpointArr[i][3].y + result.y;
    }
    else if (i==2)
    {
        result.x = prevPt.x - maskpointArr[i][2].x;
        result.y = prevPt.y - maskpointArr[i][2].y;
        //更新prevPt
        prevPt.x = maskpointArr[i][3].x + result.x;
        prevPt.y = maskpointArr[i][3].y + result.y;
    }
    else if (i == 3)
    {
        result.x = prevPt.x - maskpointArr[i][2].x;
        result.y = prevPt.y;
        //更新prevPt
        prevPt.x = maskpointArr[i][3].x + result.x;
        prevPt.y = maskpointArr[i][3].y + result.y;
    }
    else if (i == 4)
    {
        result.x = prevPt.x;
        result.y = prevPt.y - maskpointArr[i][2].y;
        //更新prevPt
        prevPt.x = maskpointArr[i][3].x + result.x;
        prevPt.y = maskpointArr[i][3].y + result.y;
    }
    else if (i == 5)
    {
        result.x = prevPt.x - maskpointArr[i][2].x;
        result.y = prevPt.y - maskpointArr[i][2].y;
        //更新prevPt
        prevPt.x = maskpointArr[i][3].x + result.x;
        prevPt.y = maskpointArr[i][3].y + result.y;
    }
    else
    {
        cout << "void MergeImg::GetResultPt err,Not index exist!!!" << endl;
    }
    return result;
}
void MergeImg::Merge(std::string imgPath, int i){
    //第二版本
    // 加载原始图像和目标图像
    cv::Mat sourceImage = cv::imread(imgPath);
    // 定义五边形顶点坐标
    std::vector<cv::Point> pentagonPoints = rotatedpointArr[i];
    // 创建一个掩模
    cv::Mat mask = cv::Mat::zeros(sourceImage.size(), CV_8UC1);
    // 在掩模上绘制五边形
    cv::fillConvexPoly(mask, pentagonPoints.data(), pentagonPoints.size(), cv::Scalar(255));
    // 创建源区域矩形
    cv::Rect sourceRect = cv::boundingRect(pentagonPoints);
    //填充maskPointArr，点集为相对于矩形的坐标
    fillMaskPoints(sourceRect,i);
    //计算x,y
    Point result = GetResultPt(sourceRect,i);
    CopyImgBuyMask(result.x,result.y,sourceRect,mask,sourceImage,i);
}
void MergeImg::CopyImgBuyMask(int x, int y, cv::Rect& sourceRect, cv::Mat& mask, cv::Mat& sourceImage,int i)
{
    // 将五边形从源图像中提取出来
    cv::Mat extractedPentagon = sourceImage(sourceRect).clone();
    cv::Mat extractedMask = mask(sourceRect).clone();
    // 创建目标区域矩形
    cv::Rect targetRect(x, y, sourceRect.width, sourceRect.height);
    // 将提取出的五边形放到目标图像中指定位置
    extractedPentagon.copyTo(dstImg(targetRect), extractedMask);
    QString resultDir = "E:\\jointImg\\src2Pic\\resultDir";
    QDir dir(resultDir);
    // 检查目录是否存在，如果不存在则创建
    if (!dir.exists())
        dir.mkdir(resultDir);
    std::string id = to_string(i);
    std::string resultImg2 = resultDir.toLocal8Bit().toStdString();
    std::string resultImg = resultImg2 + "\\result-" + id + ".jpg";
    imwrite(resultImg,dstImg);
}
//第一版
//void MergeImg::Merge(std::string imgPath, int i)
//{
//
//////// 加载原始图像和目标图像
////cv::Mat sourceImage = cv::imread(imgPath);
////
//////进行图像仿射变化
//////根据定义的三个点进行仿射变换
////Point2f src_points[3];
////Point2f dst_points[3];
////src_points[0] = Point2f(rotatedpointArr[i][2]); //要融合图像的三个点C、D、E
////src_points[1] = Point2f(rotatedpointArr[i][3]);
////src_points[2] = Point2f(rotatedpointArr[i][4]);
////Mat rotation1, warpMat;
//////放射变换后图像中的三个点
////dst_points[0] = prevPt;//上一次融合后的D点
////dst_points[1] = src_points[1] + (dst_points[0] - src_points[0]);
////dst_points[2] = src_points[2] + (dst_points[0] - src_points[0]);
////
//////更新prevPt,为下次融合做准备
////prevPt = dst_points[1];//D
////
////rotation1 = getAffineTransform(src_points, dst_points); //根据对应点求取仿射变换矩阵
////warpAffine(sourceImage, warpMat, rotation1, dstImg.size()); //进行仿射变换
//
//////叠加图像
////double alpha = 0.5;
////addWeighted(warpMat, alpha, dstImg, 1 - alpha, 0, dstImg);
//
//
//
////QString resultDir = "E:\\jointImg\\src2Pic\\resultDir";
////QDir dir(resultDir);
////// 检查目录是否存在，如果不存在则创建
////if (!dir.exists())
////dir.mkdir(resultDir);
////std::string id = to_string(i);
////std::string resultImg2 = resultDir.toLocal8Bit().toStdString();
////std::string resultImg = resultImg2 + "\\result-" + id + ".jpg";
//////对比度和亮度调整
//
////float alpha1 = 1.6; // 亮度增益因子
////int beta1 = 30; // 亮度增益偏移量
////dstImg.convertTo(dstImg, -1, alpha1, beta1);
////imwrite(resultImg, dstImg);
//
//
//
//
//}
MergeImg::~MergeImg()
{
}
